home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / scripts / mysql_fix_privilege_tables < prev    next >
Encoding:
Text File  |  2004-10-28  |  10.1 KB  |  293 lines

  1. #!/bin/sh
  2.  
  3. #
  4. # Copyright (C) 2004 MySQL AB
  5. # For a more info consult the file COPYRIGHT distributed with this file.
  6. #
  7. # This script converts any old privilege tables to privilege tables suitable
  8. # for MySQL 4.0.
  9. #
  10. # You can safely ignore all 'Duplicate column' and 'Unknown column' errors"
  11. # as this just means that your tables where already up to date.
  12. # This script is safe to run even if your tables are already up to date!
  13. #
  14. # On windows you should do 'mysql --force < mysql_fix_privilege_tables.sql' 
  15. # instead of this script
  16. #
  17. # Usage:
  18. #    mysql_fix_privilege_tables
  19. #     - fix tables for host "localhost" as "root" with no password
  20. #    mysql_fix_privilege_tables <password>
  21. #     - fix tables for host "localhost" as "root" with <password>
  22. #    mysql_fix_privilege_tables --sql-only
  23. #     - output sql-script to file /usr/share/mysql/echo_stderr
  24. #    mysql_fix_privilege_tables OPTIONS
  25. #     - fix tables on connection with OPTIONS
  26. #
  27. # where OPTIONS are 
  28. #   --host=<host>
  29. #   --port=<port>
  30. #   --socket=<socket>
  31. #   --user=<user>
  32. #   --password=<password>
  33. #   --database=<database>
  34.  
  35. root_password=""
  36. host="localhost"
  37. user="root"
  38. port=""
  39. socket=""
  40. comment=""
  41. database="mysql"
  42. bindir="/usr/local/mysql/bin"
  43.  
  44. # Old format where there is only one argument and it's the password
  45. if test "$#" = 1
  46. then
  47.   case "$1" in
  48.   --*) ;;
  49.   *) root_password="$1" ; shift ;;
  50.   esac
  51. fi
  52.  
  53. # read all the options
  54. parse_arguments() 
  55. {
  56.   for arg do
  57.     case "$arg" in
  58.       --sql-only) cmd="/usr/share/mysql/echo_stderr" ;;
  59.       --port=*) port=`echo "$arg" | sed -e "s;--port=;;"` ;;
  60.       --user=*) user=`echo "$arg" | sed -e "s;--user=;;"` ;;
  61.       --host=*) host=`echo "$arg" | sed -e "s;--host=;;"` ;;
  62.       --socket=*) socket=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  63.       --password=*) root_password=`echo "$arg" | sed -e "s;--password=;;"` ;;
  64.       --database=*) database=`echo "$arg" | sed -e "s;--database=;;"` ;;
  65.       --bindir=*) bindir=`echo "$arg" | sed -e "s;--bindir=;;"` ;;
  66.       *)
  67.         echo "Unknown argument '$arg'"
  68.         exit 1
  69.       ;;
  70.     esac
  71.   done
  72. }
  73.                                 
  74. parse_arguments "$@"
  75.  
  76. if test -z "$cmd"; then
  77.   cmd="$bindir/mysql -f --user=$user --host=$host"
  78.   if test ! -z "$root_password"; then
  79.     cmd="$cmd --password=$root_password"
  80.   fi
  81.   if test ! -z "$port"; then
  82.     cmd="$cmd --port=$port"
  83.   fi
  84.   if test ! -z "$socket"; then
  85.     cmd="$cmd --socket=$socket"
  86.   fi
  87.   cmd="$cmd $database"
  88. fi
  89.  
  90. echo "This scripts updates the mysql.user, mysql.db, mysql.host and the"
  91. echo "mysql.func tables to MySQL 3.22.14 and above."
  92. echo ""
  93. echo "This is needed if you want to use the new GRANT functions,"
  94. echo "CREATE AGGREGATE FUNCTION or want to use the more secure passwords in 3.23"
  95. echo ""
  96. echo "If you get 'Access denied' errors, you should run this script again"
  97. echo "and give the MySQL root user password as an argument!"
  98.  
  99. echo "Converting all privilege tables to MyISAM format"
  100. $cmd <<END_OF_DATA
  101. ALTER TABLE user type=MyISAM;
  102. ALTER TABLE db type=MyISAM;
  103. ALTER TABLE host type=MyISAM;
  104. ALTER TABLE func type=MyISAM;
  105. ALTER TABLE columns_priv type=MyISAM;
  106. ALTER TABLE tables_priv type=MyISAM;
  107. END_OF_DATA
  108.  
  109.  
  110. # Fix old password format, add File_priv and func table
  111. echo ""
  112. echo "If your tables are already up to date or partially up to date you will"
  113. echo "get some warnings about 'Duplicated column name'. You can safely ignore these!"
  114.  
  115. $cmd <<END_OF_DATA
  116. alter table user change password password char(16) NOT NULL;
  117. alter table user add File_priv enum('N','Y') NOT NULL;
  118. CREATE TABLE if not exists func (
  119.   name char(64) binary DEFAULT '' NOT NULL,
  120.   ret tinyint(1) DEFAULT '0' NOT NULL,
  121.   dl char(128) DEFAULT '' NOT NULL,
  122.   type enum ('function','aggregate') NOT NULL,
  123.   PRIMARY KEY (name)
  124. );
  125. END_OF_DATA
  126. echo ""
  127.  
  128. # Add the new grant colums
  129.  
  130. echo "Creating Grant Alter and Index privileges if they don't exists"
  131. echo "You can ignore any Duplicate column errors"
  132. $cmd <<END_OF_DATA
  133. alter table user add Grant_priv enum('N','Y') NOT NULL,add References_priv enum('N','Y') NOT NULL,add Index_priv enum('N','Y') NOT NULL,add Alter_priv enum('N','Y') NOT NULL;
  134. alter table host add Grant_priv enum('N','Y') NOT NULL,add References_priv enum('N','Y') NOT NULL,add Index_priv enum('N','Y') NOT NULL,add Alter_priv enum('N','Y') NOT NULL;
  135. alter table db add Grant_priv enum('N','Y') NOT NULL,add References_priv enum('N','Y') NOT NULL,add Index_priv enum('N','Y') NOT NULL,add Alter_priv enum('N','Y') NOT NULL;
  136. END_OF_DATA
  137. res=$?
  138. echo ""
  139.  
  140. # If the new grant columns didn't exists, copy File -> Grant
  141. # and Create -> Alter, Index, References
  142.  
  143. if test $res = 0
  144. then
  145.   echo "Setting default privileges for the new grant, index and alter privileges"
  146.   $cmd <<END_OF_DATA
  147.   UPDATE user SET Grant_priv=File_priv,References_priv=Create_priv,Index_priv=Create_priv,Alter_priv=Create_priv;
  148.   UPDATE db SET References_priv=Create_priv,Index_priv=Create_priv,Alter_priv=Create_priv;
  149.   UPDATE host SET References_priv=Create_priv,Index_priv=Create_priv,Alter_priv=Create_priv;
  150. END_OF_DATA
  151.   echo ""
  152. fi
  153.  
  154. #
  155. # The second alter changes ssl_type to new 4.0.2 format
  156.  
  157. echo "Adding columns needed by GRANT .. REQUIRE (openssl)"
  158. echo "You can ignore any Duplicate column errors"
  159. $cmd <<END_OF_DATA
  160. ALTER TABLE user
  161. ADD ssl_type enum('','ANY','X509', 'SPECIFIED') NOT NULL,
  162. ADD ssl_cipher BLOB NOT NULL,
  163. ADD x509_issuer BLOB NOT NULL,
  164. ADD x509_subject BLOB NOT NULL;
  165. ALTER TABLE user MODIFY ssl_type enum('','ANY','X509', 'SPECIFIED') NOT NULL;
  166. END_OF_DATA
  167. echo ""
  168.  
  169. #
  170. # Create tables_priv and columns_priv if they don't exists
  171. #
  172.  
  173. echo "Creating the new table and column privilege tables"
  174.  
  175. $cmd <<END_OF_DATA
  176. CREATE TABLE IF NOT EXISTS tables_priv (
  177.   Host char(60) DEFAULT '' NOT NULL,
  178.   Db char(60) DEFAULT '' NOT NULL,
  179.   User char(16) DEFAULT '' NOT NULL,
  180.   Table_name char(60) DEFAULT '' NOT NULL,
  181.   Grantor char(77) DEFAULT '' NOT NULL,
  182.   Timestamp timestamp(14),
  183.   Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,
  184.   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,
  185.   PRIMARY KEY (Host,Db,User,Table_name)
  186. );
  187. CREATE TABLE IF NOT EXISTS columns_priv (
  188.   Host char(60) DEFAULT '' NOT NULL,
  189.   Db char(60) DEFAULT '' NOT NULL,
  190.   User char(16) DEFAULT '' NOT NULL,
  191.   Table_name char(60) DEFAULT '' NOT NULL,
  192.   Column_name char(59) DEFAULT '' NOT NULL,
  193.   Timestamp timestamp(14),
  194.   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,
  195.   PRIMARY KEY (Host,Db,User,Table_name,Column_name)
  196. );
  197. END_OF_DATA
  198.  
  199. #
  200. # Name change of Type -> Column_priv from MySQL 3.22.12
  201. #
  202.  
  203. echo "Changing name of columns_priv.Type -> columns_priv.Column_priv"
  204. echo "You can ignore any Unknown column errors from this"
  205.  
  206. $cmd <<END_OF_DATA
  207. ALTER TABLE columns_priv change Type Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL;
  208. END_OF_DATA
  209. echo ""
  210.  
  211. #
  212. # Add the new 'type' column to the func table.
  213. #
  214.  
  215. echo "Fixing the func table"
  216. echo "You can ignore any Duplicate column errors"
  217.  
  218. $cmd <<EOF
  219. alter table func add type enum ('function','aggregate') NOT NULL;
  220. EOF
  221. echo ""
  222.  
  223. #
  224. # Change the user,db and host tables to MySQL 4.0 format
  225. #
  226.  
  227. echo "Adding new fields used by MySQL 4.0.2 to the privilege tables"
  228. echo "You can ignore any Duplicate column errors"
  229.  
  230. $cmd <<END_OF_DATA
  231. alter table user
  232. add Show_db_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER alter_priv,
  233. add Super_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Show_db_priv,
  234. add Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Super_priv,
  235. add Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Create_tmp_table_priv,
  236. add Execute_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Lock_tables_priv,
  237. add Repl_slave_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Execute_priv,
  238. add Repl_client_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Repl_slave_priv;
  239. END_OF_DATA
  240.  
  241. if test $? -eq "0"
  242. then
  243.   # Convert privileges so that users have similar privileges as before
  244.   echo ""
  245.   echo "Updating new privileges in MySQL 4.0.2 from old ones"
  246.   $cmd <<END_OF_DATA
  247.   update user set show_db_priv= select_priv, super_priv=process_priv, execute_priv=process_priv, create_tmp_table_priv='Y', Lock_tables_priv='Y', Repl_slave_priv=file_priv, Repl_client_priv=file_priv where user<>"";
  248. END_OF_DATA
  249.   echo ""
  250. fi
  251.  
  252. # Add fields that can be used to limit number of questions and connections
  253. # for some users.
  254.  
  255. $cmd <<END_OF_DATA
  256. alter table user
  257. add max_questions int(11) NOT NULL AFTER x509_subject,
  258. add max_updates   int(11) unsigned NOT NULL AFTER max_questions,
  259. add max_connections int(11) unsigned NOT NULL AFTER max_updates;
  260. END_OF_DATA
  261.  
  262. #
  263. # Add Create_tmp_table_priv and Lock_tables_priv to db and host
  264. #
  265.  
  266. $cmd <<END_OF_DATA
  267. alter table db
  268. add Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,
  269. add Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL;
  270. alter table host
  271. add Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,
  272. add Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL;
  273. END_OF_DATA
  274.  
  275. #
  276. # Fix the new bugs discovered by new tests (for Bug #2874 Grant table bugs ) 
  277. #
  278. $cmd <<END_OF_DATA
  279. alter table db change Db Db char(64) binary DEFAULT '' NOT NULL;
  280. alter table host change Db Db char(64) binary DEFAULT '' NOT NULL;
  281. alter table user change password Password char(16) binary NOT NULL, change max_questions max_questions int(11) unsigned DEFAULT 0  NOT NULL;
  282. alter table tables_priv change Db Db char(64) binary DEFAULT '' NOT NULL, change Host Host char(60) binary DEFAULT '' NOT NULL, change User User char(16) binary DEFAULT '' NOT NULL, change Table_name Table_name char(64) binary DEFAULT '' NOT NULL;
  283. alter table tables_priv add KEY Grantor (Grantor);
  284. alter table columns_priv change Db Db char(64) binary DEFAULT '' NOT NULL, change Host Host char(60) binary DEFAULT '' NOT NULL, change User User char(16) binary DEFAULT '' NOT NULL, change Table_name Table_name char(64) binary DEFAULT '' NOT NULL, change Column_name Column_name char(64) binary DEFAULT '' NOT NULL;
  285.   
  286. alter table db comment='Database privileges';
  287. alter table host comment='Host privileges;  Merged with database privileges';
  288. alter table user comment='Users and global privileges';
  289. alter table func comment='User defined functions';
  290. alter table tables_priv comment='Table privileges';
  291. alter table columns_priv comment='Column privileges';
  292. END_OF_DATA
  293.